package com.stormragetech.sylwanas.demo.app;

import com.stormragetech.sylwanas.demo.model.UserModel;
import com.stormragetech.sylwanas.demo.spring.service.ProducerExample;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
//@SpringBootApplication
//@ComponentScan(basePackages = {"com.stormragetech.sylwanas.demo"})
public class Launch {

    public static void main(String[] args) throws Exception {

        log.info("--------------------------------------------------");
        log.info("sylwanas demo is loading ...");
        log.info("--------------------------------------------------\n");

        // start spring service
        //
//        ConfigurableApplicationContext context = SpringApplication.run(Launch.class, args);
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/spring-config.xml"});
        context.start();

        System.out.println("");
        log.info("--------------------------------------------------");
        log.info("spring loading complete !!!");
        log.info("--------------------------------------------------\n");

//        ThreadPoolExample example = context.getBean(ThreadPoolExample.class);
//        example.start();

//        ConsumerExample consumerExample = context.getBean(ConsumerExample.class);
//        consumerExample.consume();

        final ProducerExample senderExample = context.getBean(ProducerExample.class);
//        senderExample.send(new UserModel(1, "" + System.nanoTime()));

        new Thread(new Runnable() {
            int id = 0;

            @Override
            public void run() {
                while (true) {
                    senderExample.send(new UserModel(id++, "" + System.nanoTime()));
                    System.out.println("send message is: " + id);

                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        log.info("--------------------------------------------------");
        log.info("sylwanas demo started !!!");
        log.info("--------------------------------------------------\n");

        try {
            System.in.read();   // 按任意键退出
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
